/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import DTO.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 *
 * @author Phong
 */
public class XuLyLinhTinhDAO {

    public static UserList DangNhap(String TenTK, String MatKhau) {
         String sql;
        UserList us = new UserList();
        try {
            Connection con = DBConnect.Connect();
             sql = String.format("Select * From userlist Where username like '%s' And pass like '%s'",TenTK,MatKhau);
           //   Connection conn = DBConnect.Connect();
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(sql);
            
           // String sql = "Select * From userlist Where username like ? And pass like ?";

        //    PreparedStatement ps = con.prepareStatement(sql);
//            ps.setString(1, TenTK);
//            ps.setString(2, MatKhau);
          //  ResultSet rs = ps.executeQuery();

            if (rs.next()) {
                us.Id = rs.getInt("id_nd");
                us.IdGroup = rs.getString("idgroup");
                us.KichHoat = rs.getInt("kichhoat");
                us.NgayTao = rs.getString("ngaytao");
                us.Pass = rs.getString("pass");
                us.UserName = rs.getString("username");
            }

        } catch (Exception ex) {
        }
        return us;
    }

    public static String TenGroup(String ID) {
        String Ten = "";
        try {
            Connection con = DBConnect.Connect();
            String sql = "Select gr.ten From groupdetail gr Where gr.idgroup like ? ";

            PreparedStatement ps = con.prepareStatement(sql);
            ps.setString(1, ID);
            ResultSet rs = ps.executeQuery();

            if (rs.next()) {
                Ten = rs.getString("ten");
            }

        } catch (Exception ex) {
        }
        return Ten;
    }
    public static NguoiDung TimNguoiDung(String ID) {
        NguoiDung nd = new NguoiDung();
        try {
            Connection con = DBConnect.Connect();
            String sql = "Select * From nguoi_dung nd Where nd.id like ? ";

            PreparedStatement ps = con.prepareStatement(sql);
            ps.setString(1, ID);
            ResultSet rs = ps.executeQuery();

            if (rs.next()) {
                nd.CMND=rs.getString("so_cmnd");
                nd.DiaChi=rs.getString("dia_chi");
                nd.DienThoai=rs.getString("dien_thoai");
                nd.GioiTinh=rs.getString("gioi_tinh");
                nd.HoTen=rs.getString("ho_ten");
                nd.Id= rs.getInt("id");
                nd.LoaiNguoiDung= rs.getInt("loai_nguoi_dung");
                nd.NgaySinh=rs.getString("ngay_sinh");
                nd.NgayTao=rs.getString("ngay_tao");
            }

        } catch (Exception ex) {
        }
        return nd;
    }
}
